//
//  UIColor+YSExtension.m
//  YSKitDemo
//
//  Created by MOLBASE on 2018/8/9.
//  Copyright © 2018年 YangShen. All rights reserved.
//

#import "UIColor+YSExtension.h"

@implementation UIColor (YSExtension)

+ (instancetype)ys_randomColor {
    return [self ys_randomColorWithAlpha:1.f];
}

+ (instancetype)ys_randomColorWithAlpha:(CGFloat)alpha {
    CGFloat r = arc4random_uniform(256);
    CGFloat g = arc4random_uniform(256);
    CGFloat b = arc4random_uniform(256);
    
    return [UIColor ys_colorWithRed:r green:g blue:b alpha:alpha];
}

+ (instancetype)ys_colorWithRed:(CGFloat)red green:(CGFloat)green blue:(CGFloat)blue {
    return [UIColor ys_colorWithRed:red green:green blue:blue alpha:1];
}

+ (instancetype)ys_colorWithRed:(CGFloat)red green:(CGFloat)green blue:(CGFloat)blue alpha:(CGFloat)alpha {
    return [UIColor colorWithRed:red / 255.0f green:green / 255.0f blue:blue / 255.0f alpha:alpha];
}

+ (instancetype)ys_colorFromHexString:(NSString *)hex {
    return [self ys_colorFromHexString:hex alpha:1.f];
}

+ (instancetype)ys_colorFromHexString:(NSString *)hex alpha:(CGFloat)alpha {
    NSString *cString = [[hex stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] uppercaseString];
    if ([cString hasPrefix:@"#"]) {
        cString = [cString substringFromIndex:1];
    } else if ([cString hasPrefix:@"0X"]) {
        cString = [cString substringFromIndex:2];
    }
    NSUInteger length = [cString length];
    //         RGB            RRGGBB
    if (length != 3 && length != 6) {
        return nil;
    }
    
    CGFloat r, g, b;
    NSUInteger digits = length / 3;
    CGFloat maxValue = (digits == 1) ? 15.0 : 255.0;
    
    r = hexStrToInt([cString substringWithRange:NSMakeRange(0, digits)]) / maxValue;
    g = hexStrToInt([cString substringWithRange:NSMakeRange(digits, digits)]) / maxValue;
    b = hexStrToInt([cString substringWithRange:NSMakeRange(2*digits, digits)]) / maxValue;
    return [UIColor colorWithRed:r green:g blue:b alpha:alpha];
}

static inline NSUInteger hexStrToInt(NSString *str) {
    uint32_t result = 0;
    sscanf([str UTF8String], "%X", &result);
    return result;
}

- (NSArray<NSNumber *> *)getRGBComponents {
    CGColorSpaceRef rgbColorSpace = CGColorSpaceCreateDeviceRGB();
    unsigned char resultingPixel[4];
    CGContextRef context = CGBitmapContextCreate(&resultingPixel,
                                                 1,
                                                 1,
                                                 8,
                                                 4,
                                                 rgbColorSpace,
                                                 1);
    CGContextSetFillColorWithColor(context, [self CGColor]);
    CGContextFillRect(context, CGRectMake(0, 0, 1, 1));
    CGContextRelease(context);
    CGColorSpaceRelease(rgbColorSpace);

    return @[@(resultingPixel[0] / 255.0f), @(resultingPixel[1] / 255.0f), @(resultingPixel[2] / 255.0f)];
}

@end
